What is natural-compare?
The natural-compare npm package provides a simple way to perform 'natural' string comparisons. This means it can compare strings in a way that is more intuitive for humans, taking into account numerical values within the strings to order them in a way that makes sense when read. For example, it would order 'item2' before 'item10', which is not the case with traditional ASCII-based string comparison methods.
What are natural-compare's main functionalities?
Natural String Comparison
This feature allows for the comparison of strings containing numerical values in a way that respects the numerical values, rather than comparing them purely as strings. The provided code sample demonstrates sorting an array of strings in a natural order using the package.
"use strict";
var naturalCompare = require('natural-compare');
console.log(['item10', 'item2'].sort(naturalCompare)); // ['item2', 'item10']
Other packages similar to natural-compare
alphanum-sort
alphanum-sort is an npm package that offers similar functionality to natural-compare, providing an algorithm for sorting strings that contain numbers in a way that 'naturally' orders them. Compared to natural-compare, alphanum-sort might offer more customization options, such as case sensitivity and whether to treat whitespace as significant.
string-natural-compare
string-natural-compare is another npm package that provides natural string comparison capabilities. It is similar to natural-compare but might offer different API options or performance characteristics. Users might choose one over the other based on specific needs or preferences in API design.
@version 1.4.0
@date 2015-10-26
@stability 3 - Stable
Natural Compare –
Compare strings containing a mix of letters and numbers
in the way a human being would in sort order.
This is described as a "natural ordering".
Standard sorting: Natural order sorting:
img1.png img1.png
img10.png img2.png
img12.png img10.png
img2.png img12.png
String.naturalCompare returns a number indicating
whether a reference string comes before or after or is the same
as the given string in sort order.
Use it with builtin sort() function.
Installation
<script src=min.natural-compare.js></script>
- In node.js:
npm install natural-compare-lite
require("natural-compare-lite")
Usage
var a = ["z1.doc", "z10.doc", "z17.doc", "z2.doc", "z23.doc", "z3.doc"];
a.sort(String.naturalCompare);
a.sort(function(a, b){
return String.naturalCompare(a.toLowerCase(), b.toLowerCase());
})
var a = [ {"street":"350 5th Ave", "room":"A-1021"}
, {"street":"350 5th Ave", "room":"A-21046-b"} ];
a.sort(function(a, b){
return String.naturalCompare(a.street, b.street) || String.naturalCompare(a.room, b.room);
})
var a = [ {"make":"Audi", "model":"A6"}
, {"make":"Kia", "model":"Rio"} ];
a.map(function(car){
car.sort_key = (car.make + " " + car.model).toLowerCase();
})
a.sort(function(a, b){
return String.naturalCompare(a.sort_key, b.sort_key);
})
- Works well with dates in ISO format eg "Rev 2012-07-26.doc".
Custom alphabet
It is possible to configure a custom alphabet
to achieve a desired order.
String.alphabet = "ABDEFGHIJKLMNOPRSŠZŽTUVÕÄÖÜXYabdefghijklmnoprsšzžtuvõäöüxy"
["t", "z", "x", "õ"].sort(String.naturalCompare)
String.alphabet = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя"
["Ё", "А", "Б"].sort(String.naturalCompare)
External links
Licence
Copyright (c) 2012-2015 Lauri Rooden <lauri@rooden.ee>
The MIT License